<template>
  <div>
    <Breadcrumb></Breadcrumb>
    <!-- 顶部区域 -->
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow"
    >
      <el-form :model="staffForm" ref="staffForm">
        <el-form-item label="工号" :label-width="formLabelWidth">
          <el-input v-model="staffForm.orderNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="staffForm.operator" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" :label-width="formLabelWidth">
          <el-input v-model="staffForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="预约时间" :label-width="formLabelWidth">
          <el-input
            type="date"
            v-model="staffForm.time"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属学校" :label-width="formLabelWidth">
          <el-input
            v-model="staffForm.totalPrice"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="负责学部" :label-width="formLabelWidth">
          <el-input
            v-model="staffForm.department"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="saveForm('staffForm')"
          >确定</el-button
        >
      </div>
    </el-dialog>
    <div class="header">
      <div class="form">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="人员搜索">
            <el-input
              v-model="formInline.name"
              @blur="blur"
              size="small"
              placeholder="请输入搜索信息"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit" size="small"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="group">
        <el-button
          class="add"
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="addSubmit"
          >添加</el-button
        >
        <el-button
          size="small"
          type="primary"
          @click="updateSubmit"
          icon="el-icon-edit"
          >编辑</el-button
        >
        <el-button
          size="small"
          type="danger"
          icon="el-icon-delete"
          @click="deleteAll"
          >删除</el-button
        >
      </div>
    </div>
    <!-- 表格区域 -->
    <div class="content">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        header-cell-class-name="active-header"
        cell-class-name="table-center"
        @select="select"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="orderNum" label="工号"> </el-table-column>
        <el-table-column prop="operator" label="姓名"> </el-table-column>
        <el-table-column prop="phone" label="联系电话"> </el-table-column>
        <el-table-column prop="time" label="预约时间">
          <template slot-scope="scope">
            {{ dayjs(scope.row.time).format("YYYY-MM-DD") }}
          </template>
        </el-table-column>
        <el-table-column prop="totalPrice" label="所属学校"> </el-table-column>
        <el-table-column prop="department" label="负责学部"> </el-table-column>
      </el-table>

      <!-- 分页 -->
      <Pagination
        :total="total"
        :pageSize="pageSize"
        @CurrentChange="CurrentChange"
      ></Pagination>
    </div>
  </div>
</template>

<script>
import dayjs from "dayjs";
import Pagination from "@/components/pagination/Pagination.vue";
import { mapMutations } from "vuex";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      operateType: "add",
      formLabelWidth: "120px",
      staffForm: {
        //表单数据
        id: "",
        orderNum: "",
        operator: "",
        phone: "",
        time: "",
        totalPrice: "",
        department: "",
        isShow: true,
      },
      isShow: false,
      formInline: {
        name: "",
      },
      tableData: [], //列表数据展示
      total: 10,
      rows: {},
      ids: [], //操作id数组集合]
      DetailsForm: [],
      pageSize: 1,
    };
  },
  methods: {
    ...mapMutations("product", ["changeRowData"]),
    dayjs,
    //批量删除-------------------------------------
    deleteAll() {
      console.log("批量删除---ids---", this.ids);
      //传递的ids必须是字符串
      let idStr = this.ids.join(",");
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //请求批量删除接口
          this.$api.batchDeleteStaff({ ids: idStr }).then((res) => {
            console.log("批量删除-------", res.data);
            if (res.data.status === 200) {
              //删除成功
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              //------批量删除-----------------------
              //获取当前是否是最后一页的数据操作-----------
              //获取当前的数据可以展示多少页码  16/8=2   17/8=3  --- 最大的页码数
              let maxPage = Math.ceil(this.total / this.pageSize);
              console.log("最大的页码数--", maxPage);
              //获取选中的个数---
              let len = this.ids.length;
              console.log("选中的个数--- ", len);
              //分析：批量删除的时候 删的位置最后一页的数据  再去比较删除的个数与最后一页的条数比较
              //删除个数==最后一页的条数 选中高亮的页码-1 请求页码数据也就是高亮页码
              if (maxPage == this.current) {
                //最后一页的批量删除操作
                //最后一页的数据条数---------------
                let num =
                  this.total % this.pageSize == 0
                    ? this.pageSize
                    : this.total % this.pageSize;
                console.log("最后一页的数据条数---", num);
                if (num === len) {
                  this.current = this.current - 1 > 0 ? this.current - 1 : 1;
                }
              }
              this.collect(this.current);
              //提示信息-------------
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addSubmit() {
      this.isShow = true; //控制弹窗
      this.operateType = "add"; //默认新增
      this.staffForm = {
        //表单数据
        id: "",
        orderNum: "",
        operator: "",
        phone: "",
        time: "",
        totalPrice: "",
        department: "",
        isShow: true,
      };
    },
    //添加---------------------------
    async addDoctor(params) {
      let res = await this.$api.addDoctor(params);
      console.log("添加列表--------", res.data);
      if (res.data.status === 200) {
        this.$message({
          message: "恭喜你，添加成功",
          type: "success",
        });
        //弹窗
        this.isShow = false;
      }
    },
    updateSubmit() {
      this.isShow = true; //控制弹窗
      this.operateType = "update"; //默认新增
      this.staffForm = this.rows; //取勾选数据行
    },
    //修改----------------------------------
    async updateDoctor(params) {
      console.log("res");
      let res = await this.$api.updateDoctor(params);
      console.log("修改", res.data);
      console.log(res.data, "状态码", res.data.status);
      if (res.data.status === 200) {
        this.$message({
          message: "恭喜你，修改成功",
          type: "success",
        });
        //弹窗
        this.isShow = false;
      }
    },
    saveForm(formName) {
      //this.$refs.staffForm
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let { id, orderNum, operator, phone, time, totalPrice, department } =
            this.staffForm;
          if (this.operateType == "add") {
            console.log("添加列表", this.staffForm);
            this.addDoctor({
              orderNum,
              operator,
              phone,
              time,
              totalPrice,
              department,
            });
          } else {
            console.log("修改列表---", this.staffForm);
            this.updateDoctor({
              id,
              orderNum,
              operator,
              phone,
              time,
              totalPrice,
              department,
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    /**搜索 */
    submit() {
      //搜索
      console.log("submit!", this.formInline.name);
      this.searchStaff(this.formInline.name);
    },
    blur() {
      //焦点去除
      if (!this.formInline.name) {
        this.collect(1);
      }
    },
    //搜索接口-----------------------------------------
    async searchStaff(search) {
      if (!search) {
        //点击空的查询x按钮
        return;
      }
      let res = await this.$api.searchStaff({ search });
      console.log("搜索数据--", res.data);
      if (res.data.status === 200) {
        //有数据
        let arr = res.data.result;
        arr.forEach((ele) => {
          ele.time = dayjs(ele.time).format("YYYY-MM-DD");
        });
        this.tableData = arr;
        //分页处理
        this.total = res.data.result.length;
        this.pageSize = res.data.result.length;
      } else {
        //查无数据
        this.tableData = [];
        this.total = 1;
        this.pageSize = 1;
      }
    },
    //选中勾选内容-----------------
    select(selection, row) {
      console.log(selection, row);
      let arr = [];
      selection.forEach((ele) => {
        arr.push(ele.id);
      });
      this.ids = arr;
      console.log(this.ids);
      //存储当前的勾选行的数据信息------
      this.DetailsForm = selection;
      this.rows = row;
    },
    //获取分页---------------------------------
    CurrentChange(page) {
      this.collect(page);
    },
    //获取校医列表---------------------
    async collect(page) {
      let res = await this.$api.collect({ page });
      console.log("校医列表---", res.data);
      if (res.data.status == 200) {
        let arr = res.data.data;
        arr.forEach((ele) => {
          ele.time = dayjs(ele.time).format("YYYY-MM-DD");
        });
        this.tableData = arr;
        this.total = res.data.total;
        this.pageSize = res.data.pageSize;
      } else {
        this.tableData = [];
        this.total = 0;
        this.pageSize = 1;
      }
    },
  },
  created() {
    this.collect();
  },
};
</script>

<style lang="less" scoped>
.header {
  background: #fff;
  margin-bottom: 20px;
  padding: 10px;

  .el-form-item {
    margin-bottom: 16px;
  }

  .group {
    border: 1px solid #eee;
    padding: 8px;
  }
}

.content {
  background: #fff;

  /deep/ .active-header {
    color: #333;
    text-align: center;
  }

  /deep/ .table-center {
    text-align: center;
  }

  .pagination {
    padding: 10px;
  }
}
</style>
